package tk.mybatis.springboot.controller;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.springboot.mapper.RegionMapper;
import tk.mybatis.springboot.mapper.WaybillMapper;
import tk.mybatis.springboot.mapper.WaybillStatusMapper;
import tk.mybatis.springboot.model.Region;
import tk.mybatis.springboot.model.Waybill;
import tk.mybatis.springboot.model.WaybillStatus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by  on 2017/4/12.
 */
@Controller
@RequestMapping("/wb")
@Transactional
public class WayBillController {

    @Autowired
    private WaybillMapper waybillMapper;
    @Autowired
    private WaybillStatusMapper waybillStatusMapper;

    @RequestMapping("/addStatus")
    public Object addStatus(WaybillStatus waybillStatus) {
        waybillStatus.setCreateTime(new Date());
        waybillStatusMapper.insertSelective(waybillStatus);
        return null;
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable String id) {
        waybillMapper.deleteByPrimaryKey(id);
        return null;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Waybill waybill) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        waybill.setCreateTime(new Date());
        waybill.setUpdateTime(new Date());
        waybill.setId(simpleDateFormat.format(new Date()) + RandomStringUtils.randomNumeric(5));
        waybill.setStatus("D");
        waybillMapper.insertSelective(waybill);
        return null;
    }
    @RequestMapping("/addzz")
    @ResponseBody
    public Object addZZ(WaybillStatus waybillStatus) {
        waybillStatus.setId(UUID.randomUUID().toString());
        waybillStatus.setCreateTime(new Date());
        waybillStatusMapper.insertSelective(waybillStatus);
        return null;
    }

    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable String id, Model model) {
        model.addAttribute("i", waybillMapper.selectByPrimaryKey(id));
        model.addAttribute("status", waybillStatusMapper.list(id));
        return "detail";
    }

    @RequestMapping("/search")
    public String search(String key, Model model) {
        model.addAttribute("list", waybillMapper.search(key));
        return "waybill/search";
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public String update(String id, String status) {
        waybillMapper.updateByPrimaryKeySelective(new Waybill(){
            {
                setId(id);
                setStatus(status);
            }
        });
        return null;
    }
}
